{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4.8 \u8df3\u8fc7\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5f00\u59cb\u90e8\u5206\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u904d\u5386\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u4f46\u662f\u5b83\u5f00\u59cb\u7684\u67d0\u4e9b\u5143\u7d20\u4f60\u5e76\u4e0d\u611f\u5174\u8da3\uff0c\u60f3\u8df3\u8fc7\u5b83\u4eec\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "itertools \u6a21\u5757\u4e2d\u6709\u4e00\u4e9b\u51fd\u6570\u53ef\u4ee5\u5b8c\u6210\u8fd9\u4e2a\u4efb\u52a1\u3002\n\u9996\u5148\u4ecb\u7ecd\u7684\u662f itertools.dropwhile() \u51fd\u6570\u3002\u4f7f\u7528\u65f6\uff0c\u4f60\u7ed9\u5b83\u4f20\u9012\u4e00\u4e2a\u51fd\u6570\u5bf9\u8c61\u548c\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u3002\n\u5b83\u4f1a\u8fd4\u56de\u4e00\u4e2a\u8fed\u4ee3\u5668\u5bf9\u8c61\uff0c\u4e22\u5f03\u539f\u6709\u5e8f\u5217\u4e2d\u76f4\u5230\u51fd\u6570\u8fd4\u56deFlase\u4e4b\u524d\u7684\u6240\u6709\u5143\u7d20\uff0c\u7136\u540e\u8fd4\u56de\u540e\u9762\u6240\u6709\u5143\u7d20\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u6f14\u793a\uff0c\u5047\u5b9a\u4f60\u5728\u8bfb\u53d6\u4e00\u4e2a\u5f00\u59cb\u90e8\u5206\u662f\u51e0\u884c\u6ce8\u91ca\u7684\u6e90\u6587\u4ef6\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('/etc/passwd') as f:\nfor line in f:\n    print(line, end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5982\u679c\u4f60\u60f3\u8df3\u8fc7\u5f00\u59cb\u90e8\u5206\u7684\u6ce8\u91ca\u884c\u7684\u8bdd\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from itertools import dropwhile\nwith open('/etc/passwd') as f:\n    for line in dropwhile(lambda line: not line.startswith('#'), f):\n        print(line, end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u4f8b\u5b50\u662f\u57fa\u4e8e\u6839\u636e\u67d0\u4e2a\u6d4b\u8bd5\u51fd\u6570\u8df3\u8fc7\u5f00\u59cb\u7684\u5143\u7d20\u3002\n\u5982\u679c\u4f60\u5df2\u7ecf\u660e\u786e\u77e5\u9053\u4e86\u8981\u8df3\u8fc7\u7684\u5143\u7d20\u7684\u5e8f\u53f7\u7684\u8bdd\uff0c\u90a3\u4e48\u53ef\u4ee5\u4f7f\u7528 itertools.islice() \u6765\u4ee3\u66ff\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from itertools import islice\nitems = ['a', 'b', 'c', 1, 4, 10, 15]\nfor x in islice(items, 3, None):\n    print(x)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c islice() \u51fd\u6570\u6700\u540e\u90a3\u4e2a None \u53c2\u6570\u6307\u5b9a\u4e86\u4f60\u8981\u8df3\u8fc7\u524d\u97623\u4e2a\u5143\u7d20\uff0c\u83b7\u53d6\u7b2c4\u4e2a\u5230\u6700\u540e\u7684\u6240\u6709\u5143\u7d20\uff0c\n\u5982\u679c None \u548c3\u7684\u4f4d\u7f6e\u5bf9\u8c03\uff0c\u610f\u601d\u5c31\u662f\u4ec5\u4ec5\u83b7\u53d6\u524d\u4e09\u4e2a\u5143\u7d20\u6070\u6070\u76f8\u53cd\uff0c\n(\u8fd9\u4e2a\u8ddf\u5207\u7247\u7684\u76f8\u53cd\u64cd\u4f5c [3:] \u548c [:3] \u539f\u7406\u662f\u4e00\u6837\u7684)\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u51fd\u6570 dropwhile() \u548c islice() \u5176\u5b9e\u5c31\u662f\u4e24\u4e2a\u5e2e\u52a9\u51fd\u6570\uff0c\u4e3a\u7684\u5c31\u662f\u907f\u514d\u5199\u51fa\u4e0b\u9762\u8fd9\u79cd\u5197\u4f59\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('/etc/passwd') as f:\n    # Skip over initial comments\n    while True:\n        line = next(f, '')\n        if not line.startswith('#'):\n            break\n\n    # Process remaining lines\n    while line:\n        # Replace with useful processing\n        print(line, end='')\n        line = next(f, None)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8df3\u8fc7\u4e00\u4e2a\u53ef\u8fed\u4ee3\u5bf9\u8c61\u7684\u5f00\u59cb\u90e8\u5206\u8ddf\u901a\u5e38\u7684\u8fc7\u6ee4\u662f\u4e0d\u540c\u7684\u3002\n\u6bd4\u5982\uff0c\u4e0a\u8ff0\u4ee3\u7801\u7684\u7b2c\u4e00\u4e2a\u90e8\u5206\u53ef\u80fd\u4f1a\u8fd9\u6837\u91cd\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "with open('/etc/passwd') as f:\n    lines = (line for line in f if not line.startswith('#'))\n    for line in lines:\n        print(line, end='')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u5199\u786e\u5b9e\u53ef\u4ee5\u8df3\u8fc7\u5f00\u59cb\u90e8\u5206\u7684\u6ce8\u91ca\u884c\uff0c\u4f46\u662f\u540c\u6837\u4e5f\u4f1a\u8df3\u8fc7\u6587\u4ef6\u4e2d\u5176\u4ed6\u6240\u6709\u7684\u6ce8\u91ca\u884c\u3002\n\u6362\u53e5\u8bdd\u8bb2\uff0c\u6211\u4eec\u7684\u89e3\u51b3\u65b9\u6848\u662f\u4ec5\u4ec5\u8df3\u8fc7\u5f00\u59cb\u90e8\u5206\u6ee1\u8db3\u6d4b\u8bd5\u6761\u4ef6\u7684\u884c\uff0c\u5728\u90a3\u4ee5\u540e\uff0c\u6240\u6709\u7684\u5143\u7d20\u4e0d\u518d\u8fdb\u884c\u6d4b\u8bd5\u548c\u8fc7\u6ee4\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u9700\u8981\u7740\u91cd\u5f3a\u8c03\u7684\u4e00\u70b9\u662f\uff0c\u672c\u8282\u7684\u65b9\u6848\u9002\u7528\u4e8e\u6240\u6709\u53ef\u8fed\u4ee3\u5bf9\u8c61\uff0c\u5305\u62ec\u90a3\u4e9b\u4e8b\u5148\u4e0d\u80fd\u786e\u5b9a\u5927\u5c0f\u7684\uff0c\n\u6bd4\u5982\u751f\u6210\u5668\uff0c\u6587\u4ef6\u53ca\u5176\u7c7b\u4f3c\u7684\u5bf9\u8c61\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}